package com.qingzhuge.web.autoconfigure;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.google.common.collect.Lists;
import com.qingzhuge.autoconfigure.QingTingProperties;
import com.qingzhuge.autoconfigure.SecurityProperties;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author : zero.xiao
 * @description :
 * @modified :
 */
@Configuration
@ConditionalOnExpression("${qingting.swagger:true}")
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {

    @Bean
    public Docket createRestApi(QingTingProperties qingTingProperties) {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .produces(produecetypes())
                .groupName("qingting-j2ee-api")
                .apiInfo(apiInfo())
                .forCodeGeneration(true)
                .ignoredParameterTypes(ApiIgnore.class)
                .select()
                //加了ApiOperation注解的类，才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //包下的类，才生成接口文档
                //.apis(RequestHandlerSelectors.basePackage("com.qingzhuge.controller"))
                .paths(PathSelectors.any())
                .build();
        SecurityProperties jwt = qingTingProperties.getSecurity();
        if (jwt.isOpenJwt()) {
            String token = jwt.getToken();
            String tokenSwgValue = jwt.getTokenSwgValue();
            //可以添加多个header或参数
            ParameterBuilder aParameterBuilder = new ParameterBuilder();
            aParameterBuilder
                    //参数类型支持header, cookie, body, query etc
                    .parameterType("header")
                    //参数名
                    .name(token)
                    //默认值
                    .defaultValue(tokenSwgValue)
                    .description("token")
                    //指定参数值的类型
                    .modelRef(new ModelRef("string"))
                    //非必需，这里是全局配置，然而在登陆的时候是不用验证的
                    .required(false).build();
            List<Parameter> aParameters = new ArrayList<>();
            aParameters.add(aParameterBuilder.build());
            docket.globalOperationParameters(aParameters);
            docket.securitySchemes(security(jwt));
        }
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("qingting-api")
                .description("©2018 Copyright. Powered By qingzhuge.com")
                .contact(new Contact("zero.xiao", "", "pub@xiaoyin.org"))
                .license("Apache License Version 2.0")
                .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
                .version("1.0.0")
                .build();
    }

    private List<SecurityScheme> security(SecurityProperties jwt) {
        String token = jwt.getToken();
        return Lists.newArrayList(
                new ApiKey(token, token, "header")
        );
    }

    private HashSet<String> produecetypes(){
        HashSet <String> hs = new HashSet<>();
        hs.add(MediaType.APPLICATION_JSON_VALUE);
        hs.add(MediaType.TEXT_HTML_VALUE);
        return hs;
    }
}